﻿using System;
using System.Collections.Generic;
using System.Linq;
using Best.SmartCms.Dao;
using Best.SmartCms.Domain;
using Best.SmartCms.Dto.Response;
using Best.SmartCms.Dto;
using Best.SmartCms.Dto.Enums;
using Best.SmartCms.Dto.Mall;
using Best.SmartCms.Service.BizException;
using Best.SmartCms.Service.Common;
using Best.SmartCms.Utility;

namespace Best.SmartCms.Service
{
    /// <summary>
    /// Product服务
    /// </summary>
    public class ProductService : IService
    {
        private readonly ProductDao _productDao;
        private readonly CategoryDao _categoryDao;
        private readonly FileService _fileService;
        private readonly WorkContext _workContext;



        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mySqlDbContext"></param>
        /// <param name="fileService"></param>
        /// <param name="workContext"></param>
        public ProductService(MySqlDbContext mySqlDbContext, FileService fileService, WorkContext workContext)
        {
            _productDao = new ProductDao(mySqlDbContext);
            _categoryDao = new CategoryDao(mySqlDbContext);
            _workContext = workContext;
            _fileService = fileService;

        }



        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResponse<ProductListDto> PageList(ProductListQueryDto query)
        {
            var list = _productDao.PageList(query, out var totalCount, out var totalPages);
            var pageList = TinyMapperExtensions.Map<List<Product>, List<ProductListDto>>(list);
            foreach (var item in pageList)
            {
                var category = _categoryDao.GetSingle(item.CategoryId);
                item.CategoryName = category?.Name;
                var images = _fileService.GetFiles(item.Id, AttachmentSourceType.Pruduct.GetHashCode());
                item.ImageUrl = images.Count > 0 ? images[0].Url : "";
            }

            return new PagedResponse<ProductListDto>(pageList, query.PageIndex,query.PageSize,totalCount,totalPages);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        public void Create(ProductEditDto dto)
        {
            var entity = TinyMapperExtensions.Map<ProductEditDto, Product>(dto);
            entity.CreatedTime = DateTime.Now;
            entity.CreatorId = _workContext.UserId;
            entity.CreatorName = _workContext.UserName;
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _productDao.Add(entity);
            _fileService.SaveFiles(dto.ImageList, entity.Id, AttachmentSourceType.Pruduct.GetHashCode());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        public void Edit(ProductEditDto dto)
        {
            var entity = _productDao.GetSingle(dto.Id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{dto.Id}的数据");
            }

            TinyMapperExtensions.Map(dto, entity);
            
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _productDao.Update(entity);
            _fileService.SaveFiles(dto.ImageList, entity.Id, AttachmentSourceType.Pruduct.GetHashCode());
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ProductEditDto Info(long id)
        {
            var entity = _productDao.GetSingle(id);
            var images = _fileService.GetFiles(id, AttachmentSourceType.Pruduct.GetHashCode());
            var dto = TinyMapperExtensions.Map<Product, ProductEditDto>(entity);
            dto.ImageList = images;
            return dto;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(long id)
        {
            var entity = _productDao.GetSingle(id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{id}的数据");
            }

            _productDao.Delete(entity);
        }

        /// <summary>
        /// 商城首页
        /// </summary>
        /// <returns></returns>
        public MallIndexDto ProductList()
        {
            var result = new MallIndexDto();
            var tjItems = _productDao.ListTuijianList();
            var hotItems = _productDao.ListHotList();
            result.TuiJianItems = TinyMapperExtensions.Map<List<Product>, List<ProductItemDto>>(tjItems);
            foreach (var item in result.TuiJianItems)
            {
                var images = _fileService.GetFiles(item.Id, AttachmentSourceType.Pruduct.GetHashCode());
                item.ImageUrl = images.Count > 0 ? images[0].Url : "";
            }

            result.HotItems = TinyMapperExtensions.Map<List<Product>, List<ProductItemDto>>(hotItems);
            foreach (var item in result.HotItems)
            {
                var images = _fileService.GetFiles(item.Id, AttachmentSourceType.Pruduct.GetHashCode());
                item.ImageUrl = images.Count > 0 ? images[0].Url : "";
            }

            return result;
        }

        /// <summary>
        /// 商品详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ProductInfoDto ProductDetail(long id)
        {
            var entity = _productDao.GetSingle(id);
            var images = _fileService.GetFiles(id, AttachmentSourceType.Pruduct.GetHashCode());
            var dto = TinyMapperExtensions.Map<Product, ProductInfoDto>(entity);
            dto.ImageList = images.Select(p=>p.Url).ToList();
            return dto;
        }
    }
}
